/*global location: false, YAHOO: false, bv_options: false, type_for_freebase_suggest: false, jQuery: false, SelectList: false, not_default_columns: false, Table: false, Filter: false, window: false, Results: false, filter_options: true */

/*members Event, ac_param, add, addListener, apply_to, background, click, 
    css, cursor, each, facebox, fields, freebaseSuggest, getColumn, 
    get_Column, get_filter_options, hide, html, insertColumn, key, live, 
    new_columns, push, rebuild, reload, removeColumn, responseSchema, rm, 
    show, siblings, submit, "text-decoration", toggle, type, util, val, 
    value, view_as, with_table
*/


/* This var is needed for determine if DataTable should start InitialLoad */


var filter = Filter;


/* Add Freebase Suggest to filter text-field */
jQuery(".filter_type").livequery("change", function (event) {
    bv_options.ac_param.type = type_for_freebase_suggest(jQuery(this).val());
    jQuery(this).siblings('input.filter_query').freebaseSuggest(bv_options);
});


/* Bind add filter action to buttons */
jQuery('input.filter_add').livequery('click', function (event) {
    filter.add();
});

/* Bind remove filter action to buttons */
jQuery('input.filter_rm').livequery('click', function (event) {
    filter.rm(this);
});

jQuery('span.article_head').livequery('click', function (event) {
    jQuery(this).css({
        'text-decoration': 'none',
        'cursor': 'default',
        'background': '#fff'
    });
    jQuery(this).siblings('span.dots').hide();
    jQuery(this).siblings('span.article_tail').show();
});

/* When user click on "Show more" link in unordered lists */
jQuery('li.edge').livequery('click', function (event) {
    var result = '<ul style="margin: 2em">';
    jQuery(this).siblings('li').each(function () {
        result += '<li>' + jQuery(this).html() + '</li>';
    });
    result += '</ul>';
    jQuery.facebox(result);
});

jQuery(document).ready(function () {


    var select_list = SelectList.new_columns(not_default_columns),
        table = Table;
    /*var searchPage = new SearchPage();*/

    /* Switch a text input to freebase-suggest field at first time */
    bv_options.ac_param.type = type_for_freebase_suggest(jQuery("select.filter_type").val());
    jQuery("input.filter_query").freebaseSuggest(bv_options);





    /* User clicks on "Filter" button */
    jQuery('input.filter_run').click(function () {
        filter_options = filter.get_filter_options();
        filter.apply_to(table);
    });


    jQuery('a#id_rm_rows').toggle(function (event) {
        select_list.rebuild();
        jQuery('select#id_row_to_add').hide();
        jQuery('input#id_button_row_add').hide();
        jQuery('select#id_row_to_rm').attr('style', 'display: inline;');
        jQuery('input#id_button_row_rm').attr('style', 'display: inline;');
    }, function () {
        select_list.rebuild();
        jQuery('select#id_row_to_add').hide();
        jQuery('input#id_button_row_add').hide();
        jQuery('select#id_row_to_rm').hide();
        jQuery('input#id_button_row_rm').hide();
    });


    jQuery('a#id_add_rows').toggle(function () {
        jQuery('select#id_row_to_add').attr('style', 'display: inline;');
        jQuery('input#id_button_row_add').attr('style', 'display: inline;');
        jQuery('select#id_row_to_rm').hide();
        jQuery('input#id_button_row_rm').hide();
        select_list.rebuild();
    }, function () {
        jQuery('select#id_row_to_add').hide();
        jQuery('input#id_button_row_add').hide();
        jQuery('select#id_row_to_rm').hide();
        jQuery('input#id_button_row_rm').hide();
        select_list.rebuild();

    })



    /* When user cliecks Add Column button */
    jQuery('#id_button_row_add').click(function (event) {
        var col_key = select_list.value();
        if (col_key === 'false') {
            return;
        }
        filter_options['col_' + col_key] = true;
        Results.responseSchema.fields.push({
            key: col_key
        });
        ResultsDataTableDeferred.insertColumn(select_list.get_Column(col_key));
        select_list.rebuild();
        table.reload();
    });


    jQuery('#id_button_row_rm').click(function (event) {
        var col_key = select_list.value();
        ResultsDataTableDeferred.removeColumn(ResultsDataTableDeferred.getColumn(col_key));
        select_list.rebuild();
    });

    /* When user clicks on 'Save' button */
    jQuery('#id_button_save').click(function () {
        var data = table.view_as('html');
        jQuery('textarea#text').val(data);
        jQuery('form#post_to_analysis').submit();
    });


});
